﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace RSentence
{
    public partial class MainWindow : Form
    {
        private Point mouseOff;
        private bool isDragging;
        private readonly Random random;
        public MainWindow()
        {
            InitializeComponent();
            random = new Random();
            RefreshSentence();
            Opacity = Properties.Settings.Default.Opacity;
        }

        private void MainWindow_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y);
                isDragging = true;
            }
        }

        private void MainWindow_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                Point mouseSet = MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void MainWindow_MouseUp(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                isDragging = false;
            }
        }

        private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new();
            about.ShowDialog();
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void MainWindow_DoubleClick(object sender, EventArgs e)
        {
            Hide();
        }

        private void SentenceLabel_Click(object sender, EventArgs e)
        {
            RefreshSentence();
        }
        private void RefreshSentence()
        {
            if (File.Exists(RSentenceFile.text))
            {
                try
                {
                    string[] strings = File.ReadAllLines(RSentenceFile.text);
                    sentenceLabel.Text = strings[random.Next(0, strings.Length - 1)];
                }
                catch
                {
                    MessageBox.Show("文件访问失败或文件为空。",
                        "错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }
            else
            {
                Directory.CreateDirectory(RSentenceFile.baseDic);
                File.Create(RSentenceFile.text);
                ShowHelp();
            }
        }

        private void HelpTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowHelp();
        }
        private static void ShowHelp()
        {
            Help help = new();
            help.ShowDialog();
        }

        private void ShowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show();
        }

        private void SettingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Setting setting = new();
            setting.ShowDialog();
        }
    }
}
